package com.supercell.misc.json;

import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import com.sun.org.apache.xpath.internal.operations.Bool;
import com.supercell.misc.data.TCPConnectionStatus;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.Collection;
import java.util.Date;
import java.util.List;

/**
 * Created by WUJO2 on 7/15/2016.
 */
public class JSONUtil {
    private static Gson gson;
    private static SimpleDateFormat dateFormat;

    static {
        gson = new Gson();
        dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    }

    private JSONUtil() {
    }

    public static String convertToJSON(Object... objects) {
        if (objects.length == 1) {
            return convertSingleObject(objects[0]);
        } else {
            return null;
        }
    }

    private static String convertSingleObject(Object object) {
        if (object instanceof Collection) {
            if (((Collection) object).size() == 0) {
                return null;
            }
            JsonArray jsonArray = new JsonArray();
            if (object instanceof List) {
                if (((List) object).get(0) instanceof TCPConnectionStatus) {
                    List statusList = (List) object;
                    for (Object listElement : statusList) {
                        TCPConnectionStatus tcpConnectionStatus = (TCPConnectionStatus) listElement;
                        JsonObject jsonObject = new JsonObject();
                        jsonObject.addProperty(TCPConnectionStatus.IP_ADDRESS, tcpConnectionStatus.getIpAddress());
                        jsonObject.addProperty(
                                TCPConnectionStatus.DATE_OF_CONNECTION,
                                dateFormat.format(tcpConnectionStatus.getDateOfConnection()));
                        jsonArray.add(jsonObject);
                    }
                    return gson.toJson(jsonArray);
                } else {
                    return null;
                }
            } else {
                return null;
            }
        } else {
            JsonObject jsonObject = new JsonObject();
            if (object instanceof Boolean) {
                boolean value = (boolean) object;
                jsonObject.addProperty("boolean", value);
                return gson.toJson(jsonObject);
            } else if (object instanceof Date) {
                Date value = (Date) object;
                jsonObject.addProperty("date", dateFormat.format(value));
                return gson.toJson(jsonObject);
            } else {
                return null;
            }
        }
    }

    public static void writeJSON(HttpServletResponse httpServletResponse, String jsonString) {
        httpServletResponse.setContentType("application/json; charset=UTF-8");
        httpServletResponse.setCharacterEncoding("UTF-8");
        PrintWriter writer;
        try {
            writer = httpServletResponse.getWriter();
        } catch (IOException e) {
            e.printStackTrace();
            return;
        }
        writer.print(jsonString);
        writer.flush();
        writer.close();
    }
}
